<?php
if (PHP_SAPI == 'cli')
	die('This example should only be run from a Web Browser');

//require_once './source/library/PHPExcel.php';
require_once IA_ROOT . '/source/library/phpexcel/PHPExcel.php';

	global $_GPC,$_W;
	$rid= intval($_GPC['rid']);
	if(empty($rid)){
		message('抱歉，传递的参数错误！','', 'error');              
	}
 	
	$list = pdo_fetchall("SELECT a.*,b.tel FROM ".tablename('bigwheel_award')." as a  left join ".tablename('bigwheel_fans')." as b on a.rid=b.rid and  a.from_user=b.from_user  WHERE a.rid = :rid and a.weid=:weid   ORDER BY a.id DESC " , array(':rid' => $rid,':weid'=>$_W['weid']));
 
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("易微营销")
							 ->setLastModifiedBy("易微营销")
							 ->setTitle("")
							 ->setSubject("")
							 ->setDescription("")
							 ->setKeywords("")
							 ->setCategory("");


// Add some data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'ID')
            ->setCellValue('B1', 'sn码')
            ->setCellValue('C1', '奖项')
            ->setCellValue('D1', '奖品名称')
            ->setCellValue('E1', '状态')
            ->setCellValue('F1', '领取者手机号')			
            ->setCellValue('G1', '中奖者微信码')					
			->setCellValue('H1', '中奖时间')
			->setCellValue('I1', '使用时间');	

$i=2;
foreach($list as $row){
if($row['status']==1){
	$row['status']='已发放';
}elseif($row['status']==2){
	$row['status']='已兑奖';
}else{
	$row['status']='未发放';
}
$objPHPExcel->setActiveSheetIndex(0)			
            ->setCellValue('A'.$i, $row['id'])
            ->setCellValue('B'.$i, $row['award_sn'])
            ->setCellValue('C'.$i, $row['prizetype'])
            ->setCellValue('D'.$i, $row['description'])
            ->setCellValue('E'.$i, $row['status'])
            ->setCellValue('F'.$i, $row['tel'])			
            ->setCellValue('G'.$i, $row['from_user'])				
			->setCellValue('H'.$i, empty($row['createtime'])?'':date('Y-m-d H:i',$row['createtime']))
            ->setCellValue('I'.$i, empty($row['consumetime'])?'':date('Y-m-d H:i',$row['consumetime']));
			
$i++;		
}					
$objPHPExcel->getActiveSheet()->getStyle('A1:I1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(22); 
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(12); 
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(14); 
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(14); 
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(18); 
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(18); 
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(18); 
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(18); 

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('活动奖品发放_'.$rid);


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="SN_'.$rid.'_'.date('Y_m_d_H_i').'.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

	